/*
 * Created by Federico Pisarello - SisDev Software (c) 2012 - fpisarello@sisdevsoft.com
 */

var actionForm = 'reportsAction.php';

var oTableAT;
var oTableRT;
var oTableRF;
var oTableTT;

function $_init()
{
	$("#sDateFrom").mask("99/99/9999",{placeholder:" "});
	$("#sDateFrom").datepicker({changeMonth: true, changeYear: true, showOn: 'button', buttonImage: 'imagenes/calendario.png', buttonImageOnly: true, dateFormat: "dd/mm/yy"});
	$("#sDateTo").mask("99/99/9999",{placeholder:" "});
	$("#sDateTo").datepicker({changeMonth: true, changeYear: true, showOn: 'button', buttonImage: 'imagenes/calendario.png', buttonImageOnly: true, dateFormat: "dd/mm/yy"});
	
	$("#btnActiveTables").click( function(e) {
		$('#rankingTables').hide();
		$('#rankingFoods').hide();
		$('#timeTable').hide();
		$('#activeTables').fadeIn();
    });
	
	$("#btnRankingTable").click( function(e) {
		$('#activeTables').hide();
		$('#rankingFoods').hide();
		$('#timeTable').hide();
		$('#rankingTables').fadeIn();
    });
	
	$("#btnRakingFoods").click( function(e) {
		$('#rankingTables').hide();
		$('#activeTables').hide();
		$('#timeTable').hide();
		$('#rankingFoods').fadeIn();
    });
	
	$("#btnTimeTables").click( function(e) {
		$('#rankingTables').hide();
		$('#rankingFoods').hide();
		$('#activeTables').hide();
		$('#timeTable').fadeIn();
    });
	
	$('#btnSearchActiveTable').click( function(e) {
		oTableAT.fnDraw();
    });
	
	$('#btnSearchRankingTable').click( function(e) {
		oTableRT.fnDraw();
    });
	
	$('#btnSearchRankingFoods').click( function(e) {
		oTableRF.fnDraw();
    });
	
	$('#btnSearchTimeTable').click( function(e) {
		oTableTT.fnDraw();
    });
	
	$.ajax({ 
	    url: actionForm, 
	    type:'POST', 
	    data: 'actionOfForm=getRestaurants', 
	    dataType: 'json', 
	    success: function(json) {
	    	$('#cmbRestaurant').append($('<option>').text('...').attr('value', 0));
	        $.each(json, function(i, value) {
	        	$('#cmbRestaurant').append($('<option>').text(value.businessName).attr('value', value.idRestaurant));
	        });
	    }
	});
	
	$_initTableAT();
	$_initTableRT();
	$_initTableRF();
	$_initTableTT();
}

function $_initTableAT()
{	
	$(document).ready(function() {
		oTableAT = $('#dt_activeTables').dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "two_button",
					"bInfo": false,
					"iDisplayLength": 100,
					"bLengthChange": true,
					"bProcessing": true,
					"bServerSide": true,
					"sAjaxSource": actionForm + "?actionOfForm=searchActiveTables",
					"aaSorting": [[3,'desc']],
					"fnServerData": function(sSource, aoData, fnCallback)
								    {
							          aoData.push(
							        		  		{ "name": "cmbRestaurant", "value": $("#cmbRestaurant").val()},
							        		  		{ "name": "dateFrom", "value": $("#sDateFrom").val()},
							        		  		{ "name": "dateTo", "value": $("#sDateTo").val()}
							        		  	 );
							          $.getJSON( sSource, aoData, function (json) { 
											fnCallback(json)
										}); 
								    },
					"aoColumns": [
					  			null,
					  			null,
					  			null
					  		]

				});
	});
}

function $_initTableRT()
{	
	$(document).ready(function() {
		oTableRT = $('#dt_rankingTables').dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "two_button",
					"bInfo": false,
					"iDisplayLength": 100,
					"bLengthChange": true,
					"bProcessing": true,
					"bServerSide": true,
					"sAjaxSource": actionForm + "?actionOfForm=searchRankingTables",
					"aaSorting": [[3,'desc']],
					"fnServerData": function(sSource, aoData, fnCallback)
								    {
							          aoData.push(
							        		  		{ "name": "cmbRestaurant", "value": $("#cmbRestaurant").val()},
							        		  		{ "name": "dateFrom", "value": $("#sDateFrom").val()},
							        		  		{ "name": "dateTo", "value": $("#sDateTo").val()}
							        		  	 );
							          $.getJSON( sSource, aoData, function (json) { 
											fnCallback(json)
										}); 
								    },
					"aoColumns": [
					  			null,
					  			null,
					  			null
					  		]

				});
	});
}

function $_initTableRF()
{	
	$(document).ready(function() {
		oTableRF = $('#dt_rankingFoods').dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "two_button",
					"bInfo": false,
					"iDisplayLength": 100,
					"bLengthChange": true,
					"bProcessing": true,
					"bServerSide": true,
					"sAjaxSource": actionForm + "?actionOfForm=searchRankingFoods",
					"aaSorting": [[3,'desc']],
					"fnServerData": function(sSource, aoData, fnCallback)
								    {
							          aoData.push(
							        		  		{ "name": "cmbRestaurant", "value": $("#cmbRestaurant").val()},
							        		  		{ "name": "dateFrom", "value": $("#sDateFrom").val()},
							        		  		{ "name": "dateTo", "value": $("#sDateTo").val()}
							        		  	 );
							          $.getJSON( sSource, aoData, function (json) { 
											fnCallback(json)
										}); 
								    },
					"aoColumns": [
					  			null,
					  			null,
					  			null
					  		]

				});
	});
}

function $_initTableTT()
{	
	$(document).ready(function() {
		oTableTT = $('#dt_timeTable').dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "two_button",
					"bInfo": false,
					"iDisplayLength": 100,
					"bLengthChange": true,
					"bProcessing": true,
					"bServerSide": true,
					"sAjaxSource": actionForm + "?actionOfForm=searchTimeTables",
					"aaSorting": [[3,'desc']],
					"fnServerData": function(sSource, aoData, fnCallback)
								    {
							          aoData.push(
							        		  		{ "name": "cmbRestaurant", "value": $("#cmbRestaurant").val()},
							        		  		{ "name": "dateFrom", "value": $("#sDateFrom").val()},
							        		  		{ "name": "dateTo", "value": $("#sDateTo").val()}
							        		  	 );
							          $.getJSON( sSource, aoData, function (json) { 
											fnCallback(json)
										}); 
								    },
					"aoColumns": [
					  			null,
					  			null,
					  			null,
					  			null
					  		]

				});
	});
}